// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.factory'])

  .run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if (window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);

      }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }
    });
  })

  .config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider

      .state('app', {
        url: '/app',
        abstract: true,
        templateUrl: 'templates/menu.html',
        controller: 'AppCtrl'
      })

      .state('app.search', {
        url: '/search',
        views: {
          'menuContent': {
            templateUrl: 'templates/search.html'
          }
        }
      })

      .state('app.browse', {
        url: '/browse',
        views: {
          'menuContent': {
            templateUrl: 'templates/browse.html'
          }
        }
      })
      .state('app.playlists', {
        url: '/playlists',
        views: {
          'menuContent': {
            templateUrl: 'templates/playlists.html',
            controller: 'PlaylistsCtrl'
          }
        }
      })
      .state('listPerformance', {
        url: '/listPerformance',
        templateUrl: 'templates/performance_list.html',
        controller: 'listPerformanceCtr',
        params: {
          title: 0,
          difference: 0,
          d1: 0,
          d2: 0

        },
        resolve: {
          preData: function ($q) {
            var deferred = $q.defer();
            deferred.resolve('aaaa');
            return deferred.promise;
          }
        }
      })
      .state('itemPerformance', {
        url: '/itemPerformance',
        templateUrl: 'templates/performance_item.html',
        controller: 'itemPerformanceCtr',
        params: {
          nums: 0,
          list: []
        },
        resolve: {
          preData: function ($q, $stateParams) {
            var deferred = $q.defer();
            var list1 = [];
            var list2 = [];
            var obj = {};
            obj = splitList($stateParams.list);
            deferred.resolve(obj);
            return deferred.promise;

            function splitList(list) {
              var obj = {};
              angular.forEach(list, function (item, index) {
                if (index % 2 == 0) {
                  list1.push(item);
                } else if (index % 2 == 1) {
                  list2.push(item);
                }
              });
              obj.list1 = list1;
              obj.list2 = list2;
              return obj;
            }
          }

        }

      })

      .state('itemNgIfPerformance', {
        url: '/itemNgIfPerformance',
        templateUrl: 'templates/performance_ng_if_item.html',
        controller: 'itemNgIfPerformanceCtr',
        params: {
          nums: 0,
          list: []
        },
        resolve: {
          preData: function ($q) {
            var deferred = $q.defer();
            deferred.resolve(123);
            return deferred.promise;
          }
        }
      })

      .state('app.single', {
        url: '/playlists/:playlistId',
        views: {
          'menuContent': {
            templateUrl: 'templates/playlist.html',
            controller: 'PlaylistCtrl'
          }
        }
      })
      .state('gps_test', {
        url: '/gps_test',
        templateUrl: 'templates/gps_test.html',
        controller: 'gpsTestController'
      })
      .state('pdf_view', {
        url: '/pdf_view',
        templateUrl: 'templates/pdf_view.html',
        controller: 'pdfViewController'
      });
    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/app/playlists');
  });
